/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import domain.pojo.ApartmentContract;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import util.HibernateUtil;

/**
 *
 * @author lion
 */

public class ApartmentContractDaoImpl extends ApartmentContractDao {

    private Session session = null;

    public ApartmentContractDaoImpl() {
        session = HibernateUtil.getSessionFactory().openSession();
    }

    @Override
    public List<ApartmentContract> getApartmentContracts() {
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            List<ApartmentContract> apartmentContracts = 
                    session.createQuery("from ApartmentContract").list();
            transaction.commit();
            return apartmentContracts;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public ApartmentContract getApartmentContract(int apartmentContractId) {
        Transaction transaction = null;
        try {
            transaction = session.beginTransaction();
            ApartmentContract apartmentContract = 
                    (ApartmentContract) session.get(ApartmentContract.class, apartmentContractId);
            transaction.commit();
            return apartmentContract;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public Integer insertApartmentInteger(ApartmentContract apartmentContract) {
        Transaction transaction = null;
        Integer apartmentContractId = null;
        try {
            transaction = session.beginTransaction();
            apartmentContractId = (Integer) session.save(apartmentContract);
            session.save(apartmentContract.getClient());
            session.save(apartmentContract.getApartment());
            transaction.commit();
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return apartmentContractId;
    }

    @Override
    public boolean deleteApartmentContract(ApartmentContract apartmentContract) {
        Transaction transaction = null;
        boolean result =  false;
        try {
            transaction = session.beginTransaction();
            session.delete(apartmentContract);
            transaction.commit();
            result =  true;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean updateApartmentContract(ApartmentContract apartmentContract) {
        Transaction transaction = null;
        boolean result =  false;
        try {
            transaction = session.beginTransaction();
            session.update(apartmentContract);
            transaction.commit();
            result = true;
        } catch (HibernateException e) {
            e.printStackTrace();
        }
        return result;
    }
}
